package com.bx.weixin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bx.weixin.pojo.SNSUserInfo;
import com.bx.weixin.pojo.WeixinOauth2Token;
import com.bx.weixin.service.UserService;
import com.bx.weixin.servlet.AdvancedUtil;
import com.demo.common.model.Company;
import com.demo.common.model.Relation;
import com.demo.common.model.User;
import com.jfinal.core.Controller;

public class OAuthController extends Controller
{
  static UserService service = new UserService();

  public void index()
  {
    HttpServletRequest request = getRequest();
    HttpServletResponse response = getResponse();
    String code = request.getParameter("code");
    String state = request.getParameter("state");

    if (!"authdeny".equals(code))
    {
      WeixinOauth2Token weixinOauth2Token = AdvancedUtil.getOauth2AccessToken("wx435fdd961701f449", "11abd50bad5fee31cbcfd17a9d8ea4bb", code);

      String accessToken = weixinOauth2Token.getAccessToken();

      String openId = weixinOauth2Token.getOpenId();

      SNSUserInfo snsUserInfo = AdvancedUtil.getSNSUserInfo(accessToken, openId);
      User user = (User)User.dao.findFirst("select * from bx_user u where u.weixin_code=? and u.state='1'", new Object[] { openId });
      if (user == null) {
        Long count = service.count();
        setAttr("count", count);

        Long bw = Long.valueOf(count.longValue() / 1000000L % 10L);
        if (bw.longValue() == 0L)
          setAttr("bw", "a");
        else {
          setAttr("bw", bw);
        }

        Long sw = Long.valueOf(count.longValue() / 100000L % 10L);
        if ((sw.longValue() == 0L) && (bw.longValue() == 0L))
          setAttr("sw", "a");
        else {
          setAttr("sw", sw);
        }

        Long w = Long.valueOf(count.longValue() / 10000L % 10L);
        if ((w.longValue() == 0L) && (sw.longValue() == 0L) && (bw.longValue() == 0L))
          setAttr("w", "a");
        else {
          setAttr("w", w);
        }

        Long q = Long.valueOf(count.longValue() / 1000L % 10L);
        if ((q.longValue() == 0L) && (w.longValue() == 0L) && (sw.longValue() == 0L) && (bw.longValue() == 0L))
          setAttr("q", "a");
        else {
          setAttr("q", q);
        }

        Long b = Long.valueOf(count.longValue() / 100L % 10L);
        if ((b.longValue() == 0L) && (q.longValue() == 0L) && (w.longValue() == 0L) && (sw.longValue() == 0L) && (bw.longValue() == 0L))
          setAttr("b", "a");
        else {
          setAttr("b", b);
        }

        Long s = Long.valueOf(count.longValue() / 10L % 10L);
        if ((s.longValue() == 0L) && (b.longValue() == 0L) && (q.longValue() == 0L) && (w.longValue() == 0L) && (sw.longValue() == 0L) && (bw.longValue() == 0L))
          setAttr("s", "a");
        else {
          setAttr("s", s);
        }

        Long g = Long.valueOf(count.longValue() % 10L);
        setAttr("g", g);
        List company = service.companyList();
        setAttr("company", company);
        setAttr("openId", openId);
        setAttr("key", Integer.valueOf(1));
        setAttr("logo", snsUserInfo.getHeadImgUrl());
        User us = (User)User.dao.findFirst("select * from bx_user u where u.weixin_code=?", new Object[] { openId });
        if (us == null) {
          User u = new User();
          u.setLogo(snsUserInfo.getHeadImgUrl());
          u.setState(Integer.valueOf(0));
          u.setWeixinCode(openId);
          u.save();
        }
        render("/bx/zhuce/reginster.html");
      } else {
        Company company = (Company)Company.dao.findFirst("select * from bx_company c where c.id=?", new Object[] { user.getCompanyId() });
        List list = User.dao.find("SELECT * FROM bx_user u, bx_relation r WHERE u.weixin_code = r.upvote_id AND r.target_id =? ORDER BY r.create_time DESC LIMIT 18", new Object[] { openId });
        setAttr("user", user);
        setAttr("company", company);
        setAttr("list", list);
        setAttr("size", Integer.valueOf(list.size()));
        setAttr("key", Integer.valueOf(1));
        setAttr("openId", openId);
        render("/bx/zhuce/promise.html");
      }
    }
  }

  public void friend()
  {
    HttpServletRequest request = getRequest();
    HttpServletResponse response = getResponse();
    String code = request.getParameter("code");
    String state = request.getParameter("state");

    String oldOpenId = getPara("oldOpenId");

    if (!"authdeny".equals(code))
    {
      WeixinOauth2Token weixinOauth2Token = AdvancedUtil.getOauth2AccessToken("wx435fdd961701f449", "11abd50bad5fee31cbcfd17a9d8ea4bb", code);

      String accessToken = weixinOauth2Token.getAccessToken();

      String openId = weixinOauth2Token.getOpenId();

      SNSUserInfo snsUserInfo = AdvancedUtil.getSNSUserInfo(accessToken, openId);

      Object[] paras = { oldOpenId, openId };
      Relation relation = (Relation)Relation.dao.findFirst("select * from bx_relation r where r.target_id=? and r.upvote_id= ?", paras);
      if (relation == null) {
        User us = (User)User.dao.findFirst("select * from bx_user u where u.weixin_code=?", new Object[] { openId });
        if (us == null) {
          User u = new User();
          u.setLogo(snsUserInfo.getHeadImgUrl());
          u.setState(Integer.valueOf(0));
          u.setWeixinCode(openId);
          u.save();
        }
        User user = (User)User.dao.findFirst("select * from bx_user u where u.weixin_code=?", new Object[] { oldOpenId });
        Company company = (Company)Company.dao.findFirst("select * from bx_company c where c.id=?", new Object[] { user.getCompanyId() });
        List list = User.dao.find("SELECT * FROM bx_user u, bx_relation r WHERE u.weixin_code = r.upvote_id AND r.target_id =? ORDER BY r.create_time DESC LIMIT 18", new Object[] { oldOpenId });
        setAttr("user", user);
        setAttr("company", company);
        setAttr("list", list);
        setAttr("size", Integer.valueOf(list.size()));
        setAttr("oldOpenId", oldOpenId);
        setAttr("openId", openId);
        setAttr("key", Integer.valueOf(1));
        render("/bx/yuedu/promise.html");
      } else {
        User user = (User)User.dao.findFirst("select * from bx_user u where u.weixin_code=?", new Object[] { oldOpenId });
        Company company = (Company)Company.dao.findFirst("select * from bx_company c where c.id=?", new Object[] { user.getCompanyId() });
        List list = User.dao.find("SELECT * FROM bx_user u, bx_relation r WHERE u.weixin_code = r.upvote_id AND r.target_id =? ORDER BY r.create_time DESC LIMIT 18", new Object[] { oldOpenId });
        setAttr("user", user);
        setAttr("company", company);
        setAttr("list", list);
        setAttr("openId", openId);
        setAttr("oldOpenId", oldOpenId);
        setAttr("size", Integer.valueOf(list.size()));
        setAttr("key", Integer.valueOf(1));
        render("/bx/yuedu/promised.html");
      }
    }
  }
}